#include "StdAfx.h"
#include "sphelper.h"
#include <iostream>
#include "SAPIModulator.h"

SAPIModulator::SAPIModulator(void)
{
	endReceived = false;
}

SAPIModulator::~SAPIModulator(void)
{
}

void SAPIModulator::Initialize(unsigned int sampleRate, unsigned int windowLength, WCHAR* voiceName)
{
	sapiController.Initialize(this, this, sampleRate, voiceName);
	this->windowLength = windowLength;
}

bool SAPIModulator::NextModulatorSample(short* output, unsigned int length, std::vector<SampleMarker>* markersReceiver)
{
	while(samples.size()<length)
	{
		Sleep(1);
	}

	unsigned int i=0;
	mutex.Lock();
	for(; i<length && samples.size(); i++)
	{
		output[i] = samples.front();
		samples.pop();
	}
	mutex.Unlock();
	return !endReceived;
}

void SAPIModulator::NotifySAPIEventCallback(CSpEvent* e)
{
	if(e->eEventId==SPEI_END_INPUT_STREAM)
	{
		endReceived = true;
	}
	else if(e->eEventId==SPEI_START_INPUT_STREAM)
	{
		endReceived = false;
	}
	else
	{
		std::cout << e->eEventId << std::endl;
	}
}



void SAPIModulator::NewSample(short* sample, unsigned int length)
{
	mutex.Lock();
	for(unsigned int i=0; i<length; i++)
	{
		samples.push(sample[i]);
	}
	mutex.Unlock();
}
